using System;
using System.Collections.Generic;
using System.Text;
using XNALibrary.Game;
using Microsoft.Xna.Framework.Graphics;

namespace XNALibrary.Lighting
{
    public class Phong : BaseEffect
    {
        public Phong()
        {
            effect = BaseGame.content.Load<Effect>("Resources\\Effects\\Lighting\\Phong");
        }

        public override void ChangeEffectUsedByModel(Model model)
        {
            Dictionary<Effect, Effect> effectDictionary = new Dictionary<Effect, Effect>();
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect oldEffect in mesh.Effects)
                {
                    if (!effectDictionary.ContainsKey(oldEffect))
                    {
                        Effect newEffect = effect.Clone(effect.GraphicsDevice);
                        effectDictionary.Add(oldEffect, newEffect);
                    }
                }

                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                {
                    meshPart.Effect = effectDictionary[meshPart.Effect];
                    effectMapping.Add(meshPart, meshPart.Effect);
                }
            }
        }

        public override void UpdateEffectParameters(Microsoft.Xna.Framework.Graphics.Effect effect, Microsoft.Xna.Framework.Matrix world)
        {
            effect.Parameters["World"].SetValue(world);
            effect.Parameters["View"].SetValue(BaseGame.camera.View);
            effect.Parameters["Projection"].SetValue(BaseGame.camera.Projection);
            effect.Parameters["cameraPosition"].SetValue(BaseGame.camera.Position);
            effect.Parameters["lightPosition"].SetValue(BaseGame.lights[0].Position);
            effect.Parameters["ambientLightColor"].SetValue(BaseGame.lights[0].AmbientColor);
            effect.Parameters["diffuseLightColor"].SetValue(BaseGame.lights[0].DiffuseColor);
            effect.Parameters["specularLightColor"].SetValue(BaseGame.lights[0].SpecularColor);
        }
    }
}
